{
 "metadata": {
  "signature": "sha256:12113b57f3dd9ec849fc169de6d42c92fd7f34fa4e324eb0a49aa71d56942622"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib: converting a matrix to a raster image\n",
      "======================================================================\n",
      "\n",
      "Scipy provides a command (imsave) to make a raster (png, jpg...) image\n",
      "from a 2D array, each pixel corresponding to one value of the array. Yet\n",
      "the image is black and white.\n",
      "\n",
      "Here is a recipy to do this with Matplotlib, and use a colormap to give\n",
      "color to the image."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from matplotlib.pyplot import *\n",
      "from scipy import mgrid\n",
      "\n",
      "def imsave(filename, X, **kwargs):\n",
      "    \"\"\" Homebrewed imsave to have nice colors... \"\"\"\n",
      "    figsize=(array(X.shape)/100.0)[::-1]\n",
      "    rcParams.update({'figure.figsize':figsize})\n",
      "    fig = figure(figsize=figsize)\n",
      "    axes([0,0,1,1]) # Make the plot occupy the whole canvas\n",
      "    axis('off')\n",
      "    fig.set_size_inches(figsize)\n",
      "    imshow(X,origin='lower', **kwargs)\n",
      "    savefig(filename, facecolor='black', edgecolor='black', dpi=100)\n",
      "    close(fig)\n",
      "\n",
      "\n",
      "X,Y=mgrid[-5:5:0.1,-5:5:0.1]\n",
      "Z=sin(X**2+Y**2+1e-4)/(X**2+Y**2+1e-4) # Create the data to be plotted\n",
      "imsave('imsave.png', Z, cmap=cm.hot )"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "imshow(imread('imsave.png'))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 2,
       "text": [
        "<matplotlib.image.AxesImage at 0x7f1733edf610>"
       ]
      },
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAAN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"text": [
        "<matplotlib.figure.Figure at 0x7f173882e610>"
       ]
      }
     ],
     "prompt_number": 2
    }
   ],
   "metadata": {}
  }
 ]
}